/**
 * General javascript file
 * Created on: August 20, 2009
 * For UBM Global
 * Contains normal javascript fuctions
 * used in the fornt end of the website
 */

/**
 * Bind object events on body load
 */
 $(document).ready(function(){
	if($("#goLogin").length > 0)
	{
		$("#goLogin").bind("click", loginFormSubmit);
	}
	
	if($("#loginMessage").length > 0)
	{	 
		$("#loginMessage").fadeOut(10000);
	}

	if($("#new_topic_link").length > 0)
	{		
		$('#new_topic_link').toggle(
			function () {
				$('#new_topic_form').slideToggle('slow');
				if($('#expand_type').text() == "-")
				{
					$('#expand_type').text("+");
				}
				else
				{
					$('#expand_type').text("-");
				}

			},
			function () {	
				 $('#new_topic_form').slideToggle('slow');
				 if($('#expand_type').text() == "-")
				 {
					$('#expand_type').text("+");
				 }
				 else
				 {
					$('#expand_type').text("-");
				 }
			}
		);
	}
 });

 /**
 * Function to validate the login form
 */
 function loginFormSubmit()
 {	 
	 var formStatus = "";

	 //Strip prefilled value from the password filed
	 var passwordVal = $("#Password").val().replace("-- Password --", "");	 
	 $("#Password").val(passwordVal);
	 	 
	 var formEmail = FormValiditor.checkBlankField("Email_Address");
	 var emailFormat = FormValiditor.filterFormInput("Email", "Email_Address");
	 var formPassword = FormValiditor.checkBlankField("Password");		
	 
	 if(formEmail)
	 {
		formStatus = formEmail;
	 }
	 else
	 if(emailFormat)
	 {
		formStatus = "The Email Address field should contain "+emailFormat;
	 }

	 if(formPassword)
	 {
		if(formStatus)
		 {
			formStatus += "<BR/>";
		 }
		formStatus += formPassword;
	 }

	 if(formStatus)
	 {
		$("#loginMessage").html(formStatus).fadeIn(2000).fadeOut(10000);		
		return false;
	 }
	 else
	 {
		 return true;
	 }
 }

/**
 * Function to change contact office on the contact us page 
 */
 function changeContactOffice(officeName, toEmail, officeLocation)
 {
	$('#dynamicText').text(officeName);
	$('#toName').val(officeName);
	$('#toEmail').val(toEmail);
	$('#officeAddress').text(officeLocation);
 }
 
 function refreshContactOffice()
 {
	
	$('#dynamicText').text('');
	$('#toName').val('');
	$('#toEmail').val('-');
	$('#officeAddress').text('');
	
 }
 
 
 /**
 *
 *
 *
 *
 */
 function createAccountForm()
 {
	 var Email_Address = FormValiditor.checkBlankField("_Email_Address");
	 var Password = FormValiditor.checkBlankField("_Password");
	 var rePassword = FormValiditor.checkBlankField("Re-Enter_Password");
	 var First_Name = FormValiditor.checkBlankField("First_Name");
	 var Phone_Number = FormValiditor.checkBlankField("Phone_Number");
	 var Company_Name = FormValiditor.checkBlankField("Company_Name");
	 var phoneNo = document.getElementById('Phone_Number').value
	 //var Privacy_Policy = $("#Privacy_Policy:checked");
	
	if(Email_Address)
	{
		alert(Email_Address);
		$('#_Email_Address').focus();
		return false;
	}
	
	if($('#_Email_Address').val() != '')
	{
		var emailFormat = FormValiditor.filterFormInput("Email", "_Email_Address");
		if(emailFormat)
		{
			alert("The Email Address field should contain "+emailFormat);
			$('#_Email_Address').focus();
			return false;
		}
		var splitedEmail = $('#_Email_Address').val().split('@');
		var emailNotallowed = ['gmail.com', 'yahoo.com', 'msn.com'];
		if(in_array(splitedEmail[1],emailNotallowed)===false)
		{
			alert("3rd party email ID's (@gmail @yahoo @msn etc) are not allowed, please use yourname@yourcompanydomainname");
			$('#_Email_Address').focus();
			return false;
		}
	}
	if(Password)
	{
		alert(Password);
		$('#_Password').focus();
		return false;
	}
	if(rePassword)
	{
		alert(rePassword);
		$('#Re-Enter_Password').focus();
		return false;
	}
	if($('#_Password').val() != '' && $('#Re-Enter_Password').val() != '' )
	{
		if($('#_Password').val() != $('#Re-Enter_Password').val())
		{
			alert("Password and Re-Enter password does not match.");
			$('#_Password').val("");
			$('#Re-Enter_Password').val("");
			$('#_Password').focus();
			return false;
			
		}
	}
	if(First_Name)
	{
		alert(First_Name);
		$('#First_Name').focus();
		return false;
	}
	if(Phone_Number)
	{
		alert(Phone_Number);
		$('#Phone_Number').focus();
		return false;
	}
	else if(!validatePhoneNumber(phoneNo)){
		alert( 'The phone number entered is invalid!' ); 
		$('#Phone_Number').focus();
		return false;
	}
	if(Company_Name)
	{
		alert(Company_Name);
		$('#Company_Name').focus();
		return false;
	}
	
  if(!$("#Privacy_Policy").is(":checked"))
  {
	  alert("Please accept the privacy policy.");
	  return false;
  }
	
 }
 
function validatePhoneNumber(elementValue){  
	var pattern = /^[\+\-0-9]{10,15}$/;  
	return pattern.test(elementValue);  
}  

function checkPhoneNumber(phoneNo) {  
 var phoneRE = /^\(\d{3}\) \d{3}-\d{4}$/;
 alert(phoneNo);
 if (phoneNo!='(xxx)xxx-xxxx' && phoneNo.match(phoneRE)) {  
   return true;  
 } else {   
   return false;  
 }  
}

   
/**
*
*
*
*
*
*/
 function viewPopupPage(pgid)
{
	mywindow = window.open("view-popuppage.php?pgid="+pgid+"&abc", "UBMGlobal", "width=400,height=400,resizable=no,status=0,scrollbars=yes,menubar=no,location=0");
	mywindow.moveTo(200,100);
	return false;
}


function confirmTopicDelete(queryString, topicId)
{
	var sure = confirm("Are you sure to delete the topic?\n This action will delete the topic alongwith all posts inside.");
	if(sure)
	{
		window.location.href = "forum.php?"+queryString+"&topic="+topicId+"&action=delete";
		return true;
	}
	else
		return false;
}

 function viewPopupEventPage(pgid)
{
	mywindow = window.open("view-popupeventpage.php?pgid="+pgid+"&abc", "UBMGlobal", "width=500,height=500,resizable=yes,status=0,scrollbars=yes,menubar=no,location=0");
	mywindow.moveTo(200,100);
	return false;
}

function in_array (needle, haystack, argStrict) {  
    var key = '', strict = !!argStrict; 

        for (key in haystack) {
            if (haystack[key] == needle) { 
				return false;
            }
        }
     return true;
}
