$(document).ready(function()
{
  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  
  $("#addemp").click(function() {
		xajax_loadurl('addemployee.php');
		showDiv();
		});
  $("input[name^='lotmultiplier']").blur(function(){
												  	checkValue(this);
												  });
  $("input[name^='limit']").blur(function(){
												  	checkLimit(this);
												  });
   $("input[name^='stop']").blur(function(){
												  	checkStop(this);
												  });
  $("#advancesetting").click(function(){
									  if(this.checked==true)
									  {
											$('#frmlist').css('display','block');  
										  	$('#enablesp').css('display','inline');
									  		$('#removesp').css('display','inline');
									  		$('#resetsp').css('display','inline');
									  		$('#savesp').css('display','inline');
									  }
									  else
									  {
										$('#frmlist').css('display','none');  
										$('#enablesp').css('display','none');
									  	$('#removesp').css('display','none');
									  	$('#resetsp').css('display','none');
										$('#savesp').css('display','none');
										}
									  });
  
$('#addsp').click(function() {
						   		window.location='index.php?mod=general&do=performance';
						   
						   })


$('#enablesp').click(function() {
						   		$allcheckbox=$("input[name^='spaction']");
								alert($allcheckbox.length);
						   })
$('#removesp').click(function() {
							  $allcheckbox=$("input[name^='spaction']:checked");
								if($allcheckbox.length>0)
								{
									$id='';
									for($i=0;$i<$allcheckbox.length;$i++)
									$id=$id+$allcheckbox[$i].value+',';
									$id=$id.substr(0,$id.length-1);
									$('#lightbox_content').html('Are you sure to delete selected signal providers ? <input type="button" name="yesme" id="yesme" value="Yes" onclick="removesp();">&nbsp;&nbsp;&nbsp;<input type="button" name="cancelme" id="cancelme" onclick="hideDiv();" value="No">');
									showDiv();
								}
								else
								{
									alert('Please select atleast one signal provider');
								}
					  })



});

function removesp()
{
		
								
								$allcheckbox=$("input[name^='spaction']:checked");
								if($allcheckbox.length>0)
								{
									$id='';
									for($i=0;$i<$allcheckbox.length;$i++)
									$id=$id+$allcheckbox[$i].value+',';
									$id=$id.substr(0,$id.length-1);
									if($id.length>0)
									{
										try
										{
											xajax_removesp($id);
											for($i=0;$i<$allcheckbox.length;$i++)
											{									
											$('#spsettingsrow'+$allcheckbox[$i].value).fadeOut("slow");
											$('#spsettingsrow'+$allcheckbox[$i].value).remove();
											}
									}
										catch(err)
										{
											
											$('#message').text("signal provider has not been removed successfully. Please try again.");
										}
												
									}
								}
								else
								{
									alert('Please select atleast one signal provider');
								}
								hideDiv();
}
function checkValue(ele)
{
	value=ele.value;
	if(value%100!=0 || value<100 || value>1000)
	{
	alert('Please enter number between 100 to 999 and should be multiple of 100');
	ele.value=100;
	return false
	}
}

function checkLimit(ele)
{
	value=ele.value;
	if(value%100!=0 || value<100 || value>1000)
	{
	alert('Please enter number between 100 to 999 and should be multiple of 100');
	ele.value=0;
	return false
	}
}
function checkStop(ele)
{
	value=ele.value;
	if(value%100!=0 || value<100 || value>1000)
	{
	alert('Please enter number between 100 to 999 and should be multiple of 100');
	ele.value=0;
	return false
	}
}


function addemployee()
{
	var empname=trim($('#name').val());
	var comment=trim($('#comment').val());
	var emplink=trim($('#emplink').val());
	var error=false;
	//emplname
		$("#namerror").html('');
		$("#commenterror").html('');
		$("#emplinkerror").html('');
		
		emplink=emplink.replace("http://", "");
		emplink="http://"+emplink;
		$("#emplink").val(emplink);
		
		regexp = /^[a-zA-Z0-9]+.*$/;
		if (empname.search(regexp)==-1) //if match failed
		{
			error=true;
			$("#nameerror").html("<font color='red'> Enter a valid candidate name.</font>");
		}
		
		if(empname.length>128)
		{
			error=true;
						$("#nameerror").html("<font color='red'>You have exceed maximum character limit .( 128 character max).</font>");
			return false;
		}
		
	regexp = /^[a-zA-Z0-9]+.*$/;
	
						if (comment.search(regexp)==-1) //if match failed
		{
			error=true;
			$("#commenterror").html("<font color='red'> Please enter a valid comment.</font>");
		}
		
		regexp = /^http(s)?:\/\/[a-z]+(\.[a-z]+)*(:+)?(\/.*)?$|i/;
		if (emplink.search(regexp)==-1 && emplink!='') //if match failed
		{
			error=true;
			$("#linkerror").html("<font color='red'> Invalid link (Ex: http://www.youlink.com).</font>");
			return false;
		}
		
		if(emplink=='' && comment!='')
		{
			error=true;
			$("#commenterror").html("<font color='red'> Please add a link first.( 96 character max).</font>");
			return false;
		}
		
		
		if(emplink.length>256)
		{
			error=true;
			$("#linkerror").html("<font color='red'> You have exceed maximum character limit .( 96 character max).</font>");
			return false;
		}
		
		if(comment.length>256)
		{
			error=true;
			$("#commenterror").html("<font color='red'> You have exceed maximum character limit .( 96 character max).</font>");
			return false;
		}
	
	
		
		
		if(error==false)
		{
			addTableRow2($("#newemployees"),empname,comment,emplink);
			$("#allemployees").append("<input type='hidden' name='candidatename[]' value='"+empname+"'");
			$("#allemployees").append("<input type='hidden' name='candidatecomment[]' value='"+comment+"'");
			$("#allemployees").append("<input type='hidden' name='candidatelink[]' value='"+emplink+"'");
			//xajax_addempinsession(empid,emplname,empfname,empemail,empaddress,empstate,empcity,empzip,empdob);
			hideDiv();
			$("#addemp").val('Add More Candidate');
		}
}

function addTableRow2(jQtable,f0,f1,f2){
    jQtable.each(function(){
        var $table = $(this);
        
		// Number of td's in the last table row
        var tds = '<tr>';
        for(var i = 0; i < 3; i++){
		    tds += '<td>'+eval('f'+i)+' </td>';
        }
        tds += '</tr>';
        if($('tbody', this).length > 0){
            $('tbody', this).append(tds);
        }else {
            $(this).append(tds);
        }
		var n = $('tr', this).length;
		
		if(n>0)
		n=n-1;
		$('#totalemployees').val(n);
	
   });
	
			return;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		$("#empdoberror").html("<font color='red'>The date format should be : mm/dd/yyyy.</font>");
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
			$("#empdoberror").html("<font color='red'>Please enter a valid month.</font>");
	
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
			$("#empdoberror").html("<font color='red'>Please enter a valid day.</font>");

		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		$("#empdoberror").html("<font color='red'>Please enter a valid 4 digit year between "+minYear+" and "+maxYear+'</font>');
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		$("#empdoberror").html("<font color='red'>Please enter a valid date</font>");
		return false
	}
return true
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function addallemployee(frmlist)
{
		frmlist.submit();
		return;
	totalemp=parseInt($("#totalemployees").val());
	var error=false;
	ownername=trim($("#ownername").val());
	owneremail=trim($("#owneremail").val());
	ownerusername=trim($("#ownerusername").val());
	ownerpassword=trim($("#ownerpassword").val());
	reownerpassword=trim($("#reownerpassword").val());
	ownercontact=trim($("#ownercontact").val());
	ownerphone=trim($("#ownerphone").val());
	ownerzip=trim($("#ownerzip").val());
	ownerstate=trim($("#ownerstate").val());
	ownercity=trim($("#ownercity").val());
	
		regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (ownername.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid employer name');
		return;
		}
		
		regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (ownercontact.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid contact name');
		return;
		}

	
		
		if (checkPhone(ownerphone)==false) //if match failed
		{
			error=true;
			alert('Please enter a valid phone #');
			return;
		}
		
		regexp1 = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		
		if (ownerstate.search(regexp1)==-1) //if match failed
		{
			error=true;
		alert('Please enter a valid country');
		return;
		}
		
		if (ownercity.search(regexp1)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid city');
			return ;
		}
		
		regexpzip = /^[0-9]{5,10}$/;
		if (ownerzip.search(regexpzip)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid zip');
			return;
		}

		regexp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if (owneremail.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid email address');
		return;
		}
	
		regexp = /^[a-zA-Z]+[a-zA-Z0-9\.\_]*[a-zA-Z0-9]+$/;
		if (ownerusername.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid User Name');
		return;
		}
		
		
		regexp = /^[a-zA-Z0-9]{6,20}$/;
		if (ownerpassword.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid password, min length of 6');
		return;
		}
		
		if (ownerpassword!=reownerpassword) //if match failed
		{
			error=true;
			alert('Wrong confirm password.');
		return;
		}
		
		xajax_checkusername(ownerusername);
	
	setTimeout(function(){
		var checkme=$("#usernameerror").val(); 
		if(checkme==0)
		{
			if(totalemp>1 && error==false)
			frmlist.submit();
			else
			alert('Please add atleast two employeers');	

		}
		
						},500);
}


function saveaddallemployee(frmlist)
{
	totalemp=parseInt($("#totalemployees").val());
	if(totalemp>0)
	frmlist.submit();
	else
	alert('Please add atleast one new employeers');	

		
}


function saveemployee(frmlist)
{
	totalemp=parseInt($("#totalemployees").val());
	var error=false;
	ownername=trim($("#ownername").val());
	owneremail=trim($("#owneremail").val());
	ownerusername=trim($("#ownerusername").val());
	ownerpassword=trim($("#ownerpasswordnew").val());
	ownercontactname=trim($("#ownercontact").val());
	ownerphone=trim($("#ownerphone").val());
	ownerzip=trim($("#ownerzip").val());
	ownerstate=trim($("#ownerstate").val());
	ownercity=trim($("#ownercity").val());
	reownerpassword=trim($("#reownerpassword").val());

	regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (ownername.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid employer name');
		return;
		}

		regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (ownercontactname.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid contact name');
		return;
		}

	
		
		if (checkPhone(ownerphone)==false) //if match failed
		{
			error=true;
			alert('Please enter a valid phone #');
			return;
		}
		regexp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if (owneremail.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid email address');
		return;
		}
	
	regexp = /^[a-zA-Z]+[a-zA-Z0-9\.\_]*[a-zA-Z0-9]+$/;
		if (ownerusername.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid User Name');
		return;
		}
		regexp1 = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (ownerstate.search(regexp1)==-1) //if match failed
		{
			error=true;
		alert('Please enter a valid country');
		return;
		}
		
		if (ownercity.search(regexp1)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid city');
		return;
		}
		
		regexpzip = /^[0-9]{5,10}$/;
		if (ownerzip.search(regexpzip)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid zip');
		return;
		}

		
		regexp = /^[a-zA-Z0-9]{6,20}$/;
		if (ownerpassword.search(regexp)==-1 && ownerpassword!='') //if match failed
		{
			error=true;
			alert('Please enter a valid password, min length of 6');
		return;
		}
		
		if (ownerpassword!=reownerpassword  && ownerpassword!='') //if match failed
		{
			error=true;
			alert('Wrong confirm password.');
		return;
		}
		
	$("#token").val('saveprofile');
	frmlist.submit();
	
	
}

function contactus()
{
	
var error=false;
	name=trim($("#fname").val());
	cname=trim($("#lname").val());
	email=trim($("#email").val());
	comment=trim($("#comment").val());
	phone=trim($("#phone").val());
	
	
	regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (name.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid name');
			return false;
		}
		
	regexp = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
		if (cname.search(regexp)==-1 && cname!='') //if match failed
		{
			error=true;
			alert('Please enter a valid company name');
		return false;
		}	
	
			
	regexp = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if (email.search(regexp)==-1) //if match failed
		{
			error=true;
			alert('Please enter a valid email address');
		return false;
		}

		
		if(phone=='') 
	{
	alert("Please enter your phone #");
	return false;
	}


		regexpaddress =/^[a-zA-Z0-9]+.*$/;
		if (comment.search(regexpaddress)==-1) //if match failed
		{
			error=true;
			alert('Please enter a comment');
			return false;
		}
	
	
}

function checkPhone(phone)
{
	
					regexp1 = /^[0-9]{8,15}$/;
					regexp2 = /^[\(][\+][0-9]{2}[\)][0-9]{8,15}$/;
					regexp3 = /^[\+][0-9]{2}[0-9]{8,15}$/;
					regexp4 = /^[0-9]{2}[\-][0-9]{10}$/;
					regexp5 = /^[0-9,\-]{8,15}$/;
					regexp6 = /^[0-9,\(][0-9,\-,\(,\)][0-9,\)]{10,15}$/;
					if (phone.search(regexp1)==-1 && phone.search(regexp2)==-1 && phone.search(regexp3)==-1 && phone.search(regexp4)==-1 &&phone.search(regexp5)==-1 && phone.search(regexp6)==-1) //if match failed
					{
						return false;
					}
					return true;
}


function checkZiporCity(czip)
{
	
					regexp1 = /^[0-9]{5,10}$/;
					regexp2 = /^[a-zA-Z]+[a-zA-Z\.\- ]*[a-zA-Z]+$/;
					if (czip.search(regexp1)==-1 && czip.search(regexp2)==-1 ) //if match failed
					{
						return false;
					}
					return true;
}

function checkform()
{
	totalcandidate=$('#totalemployees').val();
	title=$('#title').val();
	if(title=='')
	{
		alert('Please enter title');
		return false;
	}
	if(totalcandidate<2) 
	{
		alert('Please add atleast 2 candidates');
		
		return false;
	}
}

function active(id,status)
{
	document.getElementById('poll_id').value=id;
	document.getElementById('ope').value=status;
	document.getElementById('oper').value='status';

	document.frmlist.submit();
}

function editpoll(id)
{
	document.getElementById('poll_id').value=id;
	document.getElementById('oper').value='editpoll';
	document.frmlist.submit();
}

function editcandidates(id)
{
	document.getElementById('poll_id').value=id;
	document.getElementById('oper').value='editcandidates';
	document.frmlist.submit();
}



