var prevDirs = '';
var url = 2;
// JavaScript Document
function ValidFormDetails() {
	if(isBlank('userurl')) {
		alert('Please enter username!');
		return false;
	}
	if(getVal('userurl') != getValue('userurl')) {
		alert('Spaces not allowed in username!');
		return false;
	}
	var searchtext2 = /already/;
	var string12 = getHTML("ucheck");
	var matchPos12 = string12.search(searchtext2);
	if(matchPos12 != -1) {
		alert("User name not available! Please, Enter another user name!");
		return false;
	}
	if(isBlank('fname')) {
		alert('Please enter valid first name!');
		return false;
	}
	if(isBlank('lname')) {
		alert('Please enter valid last name!');
		return false;
	}
	var searchtext1 = /already/;
	var string1 = getHTML("chkemail");
	var matchPos1 = string1.search(searchtext1);
	if(matchPos1 != -1) {
		alert("Email ID already present in our database!");
		return false;
	}
	if(!validateEmailIDs('email')) {
		alert('Please enter valid email ID!');
		return false;
	}
	if(isBlank('country')) {
		alert('Please enter country.');
		return false;
	}
	if(isBlank('city')) {
		alert('Please enter city.');
		return false;
	}
/*	if(getVal('website')!='') {
		url = getVal('website');
		var v = new RegExp();
		v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		if (!v.test(url)) {
			alert("Please enter valid website url.");
			return false;
		}
	} */
	if(isBlank('url1') && isBlank('url2') && isBlank('url3') && isBlank('url4')) {
		alert('Please enter aleast one URL!');
		Object('url1').focus();
		return false;
	}
/*    for(i=1; i<5; i++) {
		if(getVal("url"+i)=='')
			continue;
		url = getVal('url'+i);
		var v = new RegExp();
		v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
		if (!v.test(url)) {
			alert("You must enter a valid url in portfolio links.");
			return false;
		}
	} */

	if(Object('chkterms').checked == false) {
		alert("Please Accept Terms and Conditions !");
		return false;
	}
	if(isBlank('scode')) {
		alert('Please enter security code!');
		return false;
	}
	return true;
 }

function checkDBforVals(id, col, div) {
	var xmlHttp = GetXmlHttpObject();
	var url;
	if(Object(id)) val = getValue(id); else return;
	
	if(val.length < 2) return;

	url = prevDirs + 'ajax.php?' + col + '=' + val;
	setHTML(div, '');
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);

	function stateChanged() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
			setHTML(div, xmlHttp.responseText);
		}
	}
	return '';
}

function showurl() {
	if(isBlank('url1'))
		return;
	if(url!=5)
		setVisibility('url'+(url++)+'div', 'block');
	if(url==5)
		setVisibility('addmore', 'hide');
}

function changePass() {
	if(isBlank('npass')) {
		Object('npass').focus();
		return false;
	}
	if(isBlank('cpass')) {
		Object('cpass').focus();
		return false;
	}
	if(getValue('npass')!=getVal('cpass') || getValue('cpass')!=getVal('npass') ) {
		alert('Password and confirming password must match!!');
		return false;
	}
	if(getVal('npass').length < 6) {
		s = setTimeout("setVisibility('loadingpass', 'none')", 3000);
		alert('Password must be at least 6 characters.');
		return false;
	}

	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?pass='+getVal('npass');
	xmlHttp.onreadystatechange = stateChanged2;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged2() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
			if(xmlHttp.responseText=='fail') {
				setHTML('passfaila', 'Password not changed!');
				Object('passsuccess').style.display='none';
				setVisibility('passfail', 'block');
				setTimeout("setVisibility('passfail', 'none')", 5000);
			}
			else {
				setHTML('passsuccessa', 'Password change was successful!');
				Object('passsuccess').style.display='block';
				setVisibility('passfail', 'none');
				setTimeout("setVisibility('passsuccess', 'none')", 5000);
			}
			setValue('cpass', '');
			setValue('npass', '');
		}
		return true;
	}
}

function liked(id) {
	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?liked='+id;
	xmlHttp.onreadystatechange = stateChanged2;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged2() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
//				alert(xmlHttp.responseText);
				if(xmlHttp.responseText=='fail')
					alert(' Thank you. You have already voted for this design!');
				if(xmlHttp.responseText=='success')
					alert('Thank you. Your vote has been submitted!');
				if(xmlHttp.responseText=='other')
					return;
		}
		return true;
	}
}

function fired(id) {
	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?fire='+id;
	xmlHttp.onreadystatechange = stateChanged2;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged2() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
//				alert(xmlHttp.responseText);
				if(xmlHttp.responseText=='fail')
					alert(' Thank you. You have already voted for this design!');
				if(xmlHttp.responseText=='success')
					alert('Thank you. Your vote has been submitted!');
				if(xmlHttp.responseText=='other')
					return;
		}
		return true;
	}
}

function selectPaymentMethod() {
	if(Object('paypal').checked==true) {
		$('#cc-payment-div').slideUp('slow');
		setHTML('error-div-html', 'Error! Please correct the errors.');
		$('#error-div').slideUp('slow');
		$('#cctype_error').hide();
		$('#ccnumber_error').hide();
		$('#ccnumber').val('');
		$('#zip').val('');
		$('#ccvn').val('');
	}
	if(Object('auth').checked==true) {
		setHTML('error-div-html', 'Error! Please correct the errors.');
		$('#cc-payment-div').slideUp('slow');
		$('#cctype_error').hide();
		$('#ccnumber_error').hide();
		$('#ccnumber').val('');
		$('#zip').val('');
		$('#ccvn').val('');
	}
	return;
}

/* Credit card expiration details validation */
function checkCCDates() {
	var d = new Date();
	year = d.getFullYear();
	month = d.getMonth();
	$('#monthyear_error').slideUp('slow');
	if(getVal('month') < 1 || getVal('month') > 12) {
		$('#monthyear_error').slideDown('slow');
		setHTML('monthyear_error', '<span style="color:#FF0000;">Please select valid card expiration date.</span>');
		return false;
	}
	if(getVal('year')=='') {
		$('#monthyear_error').slideDown('slow');
		setHTML('monthyear_error', '<span style="color:#FF0000;">Please select valid card expiration date.</span>');
		return false;
	}
	if(getVal('year')==year && getVal('month') < (month+1)) {
		$('#monthyear_error').slideDown('slow');
		setHTML('monthyear_error', '<span style="color:#FF0000;">Please select valid card expiration date.</span>');
		return false;
	}
}
/* Credit card expiration details validation */

/* Credit card number details validation */
function checkCCDetails() {
	if(getVal('ccnumber')=='') return;
	xmlHttp = GetXmlHttpObject();
	paymentError = 0;
	$params = 'cctype=' + getVal('cctype') + '&ccnumber=' + getVal('ccnumber');
	fullurl = root+'creditcard.php?'+$params;
	xmlHttp.onreadystatechange = stateChanged2;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);
	setHTML('error-div-html', 'Error! Please correct the errors.');
	$('#error-div').slideUp('slow');
	$('#ccnumber_error').slideUp('slow');
	$('#cctype_error').slideUp('slow');

	function stateChanged2() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
			// alert(xmlHttp.responseText);
			if(xmlHttp.responseText!='') {
				$eror = xmlHttp.responseText;
				if($eror==0) { setHTML('cctype_error', '<span style="color:#FF0000;">Unknown card type.</span>'); $('#cctype_error').slideDown('slow'); }
				if($eror==1 || $eror==2 || $eror==3 || $eror==4) {
				if($eror==1) setHTML('ccnumber_error', '<span style="color:#FF0000;">Please enter credit card number.</span>');
				if($eror==2) setHTML('ccnumber_error','<span style="color:#FF0000;">Credit card number has invalid format.</span>');
				if($eror==3) setHTML('ccnumber_error', '<span style="color:#FF0000;">Credit card number is invalid.</span>');
				if($eror==4) setHTML('ccnumber_error', '<span style="color:#FF0000;">Credit card number is wrong length.</span>');
					$('#ccnumber_error').slideDown('slow');
			    }
				$('#error-div').slideDown('slow');
				return;
			}
			$('#error-div').slideUp('slow');
			$('#ccnumber_error').html('<span style="color:#00FF00;">Credit card number valid.</span>');
			$('#ccnumber_error').slideDown('slow');
		}
	}
	return false;
}

function sendCodeMail() {
	if(isBlank('emailaddress')) {
		setHTML('wrongcodeemail', '<span style="color:#FF0000;font-size:11px;font-family:Arial, Helvetica, sans-serif;">Please enter email Id.!</span>')
		$('#wrongcodeemail').slideDown();
		return false;
	}
	if(!validateEmailIDs('emailaddress')) {
		setHTML('wrongcodeemail', '<span style="color:#FF0000;font-size:11px;font-family:Arial, Helvetica, sans-serif;">Entered email id is wrong.!</span>')
		$('#wrongcodeemail').slideDown();
		return;
	}
	setHTML('#error-div-html', 'Error! Please correct the errors.');
	$('#wrongcodeemail').slideUp();
	$('#mailDiv').hide();
	$('#loading').show();
	
	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?codesemail='+getVal('emailaddress');
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
//			alert(xmlHttp.responseText);
			if(xmlHttp.responseText=='success') {
				setHTML('wrongcodeemail', '<span style="color:#6FA02D;font-size:13px;font-family:Arial, Helvetica, sans-serif;">Mail has been sent. Please check your mail box.!</span>');
				$('#wrongcodeemail').slideDown();
			}
			if(xmlHttp.responseText=='fail') {
				setHTML('wrongcodeemail', '<span style="color:#FF0000;font-size:11px;font-family:Arial, Helvetica, sans-serif;">Provided email id not present in our order database!</span>');
				$('#wrongcodeemail').slideDown();
			}
			if(xmlHttp.responseText=='used') {
				setHTML('wrongcodeemail', '<span style="color:#FF0000;font-size:11px;font-family:Arial, Helvetica, sans-serif;">You already used all order codes.!</span>');
				$('#wrongcodeemail').slideDown();
			}
			$('#mailDiv').show();
			$('#loading').hide();
		}
	}
}

function submitPaymentForm() {
	if(document.getElementById('package199').checked==true || document.getElementById('package500').checked==true) {
		if(document.getElementById('package199').checked==true) {
			$('#item_name').val('worldtopdesigners.com job board listings (1 listing $199)');
			$('#amount').val('199');
		} else {
			$('#item_name').val('worldtopdesigners.com job board listings (5 listings $500)');
			$('#amount').val('500');
		}
	$('#notify_url').val('http://www.worldtopdesigners.com/index.php/PostJob/Notify/'+$('#jobid').val()+'/'+$('#amount').val()+'/');
		document.paypal_form.submit();
	}
	if(document.getElementById('creditcode').checked==true)
		document.creditcodeform.submit();
	return false;
}

function checkCreditCode() {
	if(getValue('code')=='') {
		$('#wrongcreditcode').slideUp();
		return;
	}
	if(getValue('code').length != 16) {
		setHTML('wrongcreditcode','Entered credit code is wrong.!');
		$('#wrongcreditcode').slideDown();
		return;
	}

	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?creditcode='+getVal('code');
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
			// alert(xmlHttp.responseText);
			if(xmlHttp.responseText=='success') {
				setHTML('wrongcreditcode', '<span style="color:#6FA02D;font-size:12px;font-family:Arial, Helvetica, sans-serif;">Entered Code accepted Please post your job details.!</span>');
				$('#wrongcreditcode').slideDown();
			}
			if(xmlHttp.responseText=='fail') {
				setHTML('wrongcreditcode', 'Provided credit code not present in our order database!');
				$('#wrongcreditcode').slideDown();
			}
			if(xmlHttp.responseText=='used') {
				setHTML('wrongcreditcode', 'Entered credit code was already used.!');
				$('#wrongcreditcode').slideDown();
			}
		}
	}
}

function delDesign(id) {
	if(!confirm('Do you want to delete this design?'))
		return false;

	xmlHttp = GetXmlHttpObject();
	fullurl = root+'ajax.php?design='+id;
	xmlHttp.onreadystatechange = stateChanged3;
	xmlHttp.open("GET", fullurl, true);
	xmlHttp.send(null);

	function stateChanged3() {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "completed") {
//				alert(xmlHttp.responseText);
				if(xmlHttp.responseText=='success') {
					alert('Design deleted successfully!');
					window.location = root+'index.php?/myaccount/index/delete/';
				}
				if(xmlHttp.responseText=='fail')
					return;
		}
		return true;
	}
}

var taf = 1;
function tellafrnd() {
	retun = true;
	blank = 0;
	for($tafi=1; $tafi<7; $tafi++) {
		if($('#taftxt'+$tafi).val()=='') { blank++; continue; }
		$op = validateEmailIDs('taftxt'+$tafi);
		if($op==false) {
			retun = false;
		}
	}
	if(blank==6 && taf==1) { alert('Please enter email Id.'); retun = false; }
	else if(blank==6 && taf>1) { alert('Please enter at least one email Id.'); retun = false; }
	return retun;
}

function subscribeNewsletter() {
	urL = root+'ajax.php?newsletteremail='+getVal('newsletteremail');
	$.get(urL, function(data) {
		if(data=='subscribed') {
			alert('You are already subscribed for newsletter.');
		} else if(data=='error') {
			alert('Error: ' + data);
		}
		else {
			alert('Thank you. You are successfully subscribed for newsletters.');
		}
	});
	return false;
}

function login(action) {
	if(action=='login') {
		if(!isBlank('loginboxemail') && !isBlank('loginboxpass')) {
			$('#loginError').slideDown('slow');
			$('#loginError').html('<img src="'+root+'system/images/loadbar.gif" alt="" title="" />');
			urL = root+'ajax.php?username='+getVal('loginboxemail')+'&password='+getVal('loginboxpass');
			$.get(urL, function(data) {
				if(data=='fail') {
					$('#loginError').html('Error! Wrong login details. Please re-try.!');
					$('#loginboxemail').focus();
				} else {
					window.location = root+'index.php?/myaccount/';
				}
			});
		}
	}
	else 
		if(action == 'forgotpass') {
			if(!isBlank('forgotemail') && validateEmailIDs('forgotemail')) {
				$('#loginError').slideDown('slow');
				$('#loginError').html('<img src="'+root+'system/images/loadbar.gif" alt="" title="" />');
				urL = root+'ajax.php?forgotemail='+getVal('forgotemail');
				$.get(urL, function(data) {
					if(data=='fail') {
						$('#loginError').html('Error! Wrong email Id. Please re-try.!');
					} else if(data=='success') {
						$('#loginError').html('<span style="color:#006600;">Mail has been sent.!</span>');
					} else {
						$('#loginError').html(data);
					}
				});
			}  else {
				$('#forgotemail').focus();
				$('#loginError').slideUp('slow');
			}
		}
}

var arr = new Array(15);
arr['jobtitle'] = '" Senior Designer " or " 3D Animator "...';
arr['companylocation'] = '" Orlando, FL " or " Anywhere "...';
arr['jobdescription'] = 'Job description...';
arr['howtodescription'] = 'Example: Send resume to name@company.com or click this link ...';
arr['companyname'] = 'Your company name here ...';
arr['url'] = 'Your company website ...';
arr['email'] = 'For confirmation and receipt only ...';

arr['srch'] = 'Search';
arr['newsletteremail'] = 'Email Address';

arr['loginboxemail'] = 'Username';
arr['loginboxpass'] = 'Password';

function chkjopbform() {
	if($('jobtitle').val()==arr['jobtitle']) $('jobtitle').val('');
	if($('companylocation').val()==arr['companylocation']) $('companylocation').val('');
	if($('jobdescription').val()==arr['jobdescription']) $('jobdescription').val('');
	if($('howtodescription').val()==arr['howtodescription']) $('howtodescription').val('');
	if($('companyname').val()==arr['companyname']) $('companyname').val('');
	if($('url').val()==arr['url']) $('url').val('');
	if($('email').val()==arr['email']) $('email').val('');
}

function focusFormEffect(id) {
	if($('#' + id).val()==arr[id]) {
		$('#' + id).val('');
		$('#' + id).css('color', '#000000');
	}
}

function blurFormEffect(id) {
	if($('#' + id).val()=='') {
		$('#' + id).val(arr[id]);
		$('#' + id).css('color', '#999999');
	}
}

$(document).ready(function() {
	$('#srch').keydown(function (e) {
		if(!e) evt = window.event;
		if(e.keyCode == 13) {
			// if($('#srch').val()=='') { alert('Please, enter search keyword.!'); return; }
			// document.searchFrm.submit();
		}
	});
	$('#forgotemail').keydown(function (e) {
		if(!e) evt = window.event;
		if(e.keyCode == 13) {
			if ($('#popup').is(':visible')) {
				if($('#forgotbx').is(':visible')) {
					login('forgotpass');
				}
				if($('#loginbx').is(':visible')) {
					login('login');
				}
			}
			else {
				return;
			}
		}
	});
	$('#loginboxemail').keydown(function (e) {
		if(!e) evt = window.event;
		if(e.keyCode == 13) {
			if ($('#popup').is(':visible')) {
				if($('#forgotbx').is(':visible')) {
					login('forgotpass');
				}
				if($('#loginbx').is(':visible')) {
					login('login');
				}
			}
			else {
				return;
			}
		}
	});
	$('#loginboxpass').keydown(function (e) {
		if(!e) evt = window.event;
		if(e.keyCode == 13) {
			if ($('#popup').is(':visible')) {
				if($('#forgotbx').is(':visible')) {
					login('forgotpass');
				}
				if($('#loginbx').is(':visible')) {
					login('login');
				}
			}
			else {
				return;
			}
		}
	});
});

/* Banner rotator : BEGIN */
function theRotator(position) {
	//Set the opacity of all images to 0
	$('div#rotator'+position+' ul li').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator'+position+' ul li:first').css({opacity: 1.0});
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate(\''+position+'\')', 6000);
}

function rotate(position) {
	//Get the first image
	var current = ($('div#rotator'+position+' ul li.show')?  $('div#rotator'+position+' ul li.show') : $('div#rotator'+position+' ul li:first'));
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator'+position+' ul li:first') :current.next()) : $('div#rotator'+position+' ul li:first'));
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
}
/* Banner rotator : END */

/* Bookmark : BOF */
/* Modified To support Opera */
function bookmark(title, url) {
	if(document.all) { // ie
		window.external.AddFavorite(url, title);
	}
	else if(window.opera && window.print) { // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	}
}
/* Bookmark : EOF */

/* Job Payment Page: EOF */
$(function() {
	radioBtns = $('#package199,#package500,#creditcode');
	$.each(radioBtns, function() {
		$(this).click(function() {
			if(this.id=='package199' || this.id=='package500') {
				$('#creditpayBx').slideUp('slow');
				if(this.id=='package199') {
					$('#selectedpackage').html('Your selected package: Pay for this post only $199');
					$('#amount').val(199);
				}
				if(this.id=='package500') {
					$('#selectedpackage').html('Your selected package: Buy a 5 jobs posting package for $500 ($100 per posting)');
					$('#amount').val(500);
				}				
			}
			if(this.id=='creditcode') {
				$('#creditpayBx').slideDown('slow');
				$('#selectedpackage').html('If you are using a credit, enter your code below.Otherwise,use your credit or debit card to pay securely via PayPal.');
				$('#amount').val('creditcode');
			}
		});
	});
});
/* Job Payment Page: BOF */
