// Javascript Functions

function page_loation(page_name)
{
	location.href=page_name;
}


//Function to validate email addresses
function validEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var str1=""; 
	if (str.indexOf(at)==-1){
	   return str1;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return str1;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return str1;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return str1;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return str1;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return str1;
	 }
	
	 if (str.indexOf(" ")!=-1){
		return str1;
	 }

	 return str;					
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
	  retValue = retValue.substring(0, retValue.length-1);
	  ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function			


//Function to numaric.
function is_it_numaric(inputString){
	var retValue = inputString;
	var str="";
	var ch="";
	var i=0;
	var found=0;
	for(i=0;i<retValue.length;i++)
	{
		ch=retValue.charAt(i);
		if(ch>='0' && ch <='9')
		{
			found=0;
		}
		else
		{
			found=1;
			return str;
			break;
		}
	}
	return 	retValue;	
}

//Function to double.
function is_it_double(inputString){
	var retValue = inputString;
	var str="";
	var ch="";
	var i=0;
	var found=0;
	for(i=0;i<retValue.length;i++)
	{
		ch=retValue.charAt(i);
		if(ch>='0' && ch <='9' || ch =='.')
		{
			found=0;
		}
		else
		{
			found=1;
			return str;
			break;
		}
	}
	return 	retValue;	
}

//Function to Phone number.
function is_it_phone(inputString){
	var retValue = inputString;
	var str="";
	var ch="";
	var i=0;
	var found=0;
	for(i=0;i<retValue.length;i++)
	{
		ch=retValue.charAt(i);
		if(ch>='0' && ch <='9' || ch =='+' || ch =='-' || ch ==',' || ch ==';' || ch =='.' || ch ==' ')
		{
			found=0;
		}
		else
		{
			found=1;
			return str;
			break;
		}
	}
	return 	retValue;	
}

//Function to validate the form valid_log_in_frm.
function check_validity_log_in_frm (log_in_frm){
	if (trim(log_in_frm.admin_user_name.value) == ""){
		alert ("Please insert user name.");
		log_in_frm.admin_user_name.focus();
		return false;
	}
	if (trim(log_in_frm.admin_user_pass.value) == ""){
		alert ("Please insert password.");
		log_in_frm.admin_user_pass.focus();
		return false;
	}
	return true;
}

//Function to validate the form valid_change_pass_frm.
function check_validity_change_pass_frm (change_pass_frm){
	if (trim(change_pass_frm.admin_user_name.value) == ""){
		alert ("user name can't be blank.");
		change_pass_frm.admin_user_name.focus();
		return false;
	}	
	if (trim(change_pass_frm.ad_new_pass.value) == ""){
		alert ("Please insert new password.");
		change_pass_frm.ad_new_pass.focus();
		return false;
	}
	if (change_pass_frm.ad_new_pass.value.length >10 ){
		alert ("Password length must be >=6 and <=10.");
		change_pass_frm.ad_new_pass.focus();
		return false;
	}
	if (change_pass_frm.ad_new_pass.value.length <6 ){
		alert ("Password length must be >=6 and <=10.");
		change_pass_frm.ad_new_pass.focus();
		return false;
	}	
	if (trim(change_pass_frm.ad_retype_pass.value) == ""){
		alert ("Please retype password.");
		change_pass_frm.ad_retype_pass.focus();
		return false;
	}
	if (change_pass_frm.ad_retype_pass.value != change_pass_frm.ad_new_pass.value){
		alert ("Retype password not match.");
		change_pass_frm.ad_retype_pass.focus();
		return false;
	}	
	if(!confirm("Are you sure you want to change\nthe user name and password?")){
		return false;
		}	
	return true;
}


function delete_protfolio_by_id(ptf_id)
{
	var answer = confirm('Are you sure you want to delete\nthat profile information forever?')
	if (answer){
		location.href='all_delete_action.php?delete_protfolio_by_id='+ptf_id;
    }
}

function show_value_for_protfolio(ptf_id,h_key,ptf_name,linked_url,is_viewed)
{
	var fldName=document.getElementById('cur_protfolio_id');
	fldName.value=ptf_id;
	var fldName=document.getElementById('project_h_key');
	fldName.value=h_key;	
	var fldName=document.getElementById('project_name');
	fldName.value=ptf_name;
	var fldName=document.getElementById('linked_url');
	fldName.value=linked_url;
	var fldName=document.getElementById('is_viewed');
	fldName.value=is_viewed;
	var fldName=document.getElementById('protfolio_submit_btn');
	fldName.value="Update";

	var fldName=document.getElementById('is_protfolio_update');
	fldName.value="1";	
}
//Function to validate the form check_validity_protfolio_save_frm.
function check_validity_protfolio_save_frm (protfolio_save_frm){
	if (is_it_numaric(protfolio_save_frm.project_h_key.value) == ""){
		alert ("Please insert valid Higher key.");
		protfolio_save_frm.project_h_key.focus();
		return false;
	}	
	if (trim(protfolio_save_frm.project_name.value) == ""){
		alert ("Please insert project name.");
		protfolio_save_frm.project_name.focus();
		return false;
	}	
	if (protfolio_save_frm.linked_url.value == "http://" || protfolio_save_frm.linked_url.value == ""){
		alert ("Please insert linked URL.");
		protfolio_save_frm.linked_url.focus();
		return false;
	}
	if (trim(protfolio_save_frm.is_protfolio_update.value) != "1"){
		if (trim(protfolio_save_frm.project_photo_file.value) == ""){
			alert ("Please Browse .jpeg, .jpg, .png, .gif file.");
			protfolio_save_frm.project_photo_file.focus();
			return false;
		}
	}
	var sm="Are you sure you want to save that project information?";
	if (trim(protfolio_save_frm.is_protfolio_update.value) == "1"){
		sm="Are you sure you want to update that project information?";
	}	
	if(!confirm(sm)){
		return false;
		}	
	return true;
}

//Function to validate the form check_validity_contactus_frm.
function check_validity_contactus_frm (contactus_frm){
	if (trim(contactus_frm.txtName.value) == ""){
		alert ("Please insert your name.");
		contactus_frm.txtName.focus();
		return false;
	}
	if (is_it_phone(contactus_frm.txtTelephone.value) == ""){
		alert ("Please insert your valid contact number.");
		contactus_frm.txtTelephone.focus();
		return false;
	}
	if (validEmail(contactus_frm.txtEmail.value) == ""){
		alert ("Please insert your valid email address.");
		contactus_frm.txtEmail.focus();
		return false;
	}
	if (trim(contactus_frm.textarea.value) == ""){
		alert ("Please write your comments.");
		contactus_frm.textarea.focus();
		return false;
	}	
	if(!confirm("Are you sure you want to send these information?")){
		return false;
		}	
	return true;
}

//Function to validate the form check_validity_contactus_frm.
function check_validity_sp_offer_save_frm (sp_offer_save_frm){
	if(sp_offer_save_frm.is_sp_offer_update.value=="0")
	{
		if (trim(sp_offer_save_frm.sp_offer_img_file.value) == ""){
			alert ("Please browse a image of special offer.");
			sp_offer_save_frm.sp_offer_img_file.focus();
			return false;
		}
	}
	
	var sm="Are you sure you want to add this special offer?";
	if(sp_offer_save_frm.is_sp_offer_update.value=="1")
	{
		sm="Are you sure you want to update this special offer?";		
	}
	if(!confirm(sm)){
		return false;
		}	
	return true;
}

function show_value_special_offer(sp_id,is_view)
{
	var fldName=document.getElementById('cur_sp_offer_id');
	fldName.value=sp_id;
	var fldName=document.getElementById('is_viewed');
	fldName.value=is_view;	
	var fldName=document.getElementById('sp_offer_submit_btn');
	fldName.value="Update";

	var fldName=document.getElementById('is_sp_offer_update');
	fldName.value="1";	
}

function delete_sp_offer_by_id(sp_id)
{
	var answer = confirm('Are you sure you want to delete\nthat special offer forever?')
	if (answer){
		location.href='all_delete_action.php?delete_sp_offer_by_id='+sp_id;
    }
}

