function doEmailCheck(field, tdiv){
	//checks if contents of a field is a valid email - if it is will make text green, if not red
	tdiv = document.getElementById(tdiv);
	if(isValidEmail(field.value)){
		try{
		tdiv.innerHTML = "";		
		field.style.color = "Green";
		field.style.borderColor = "";
		}catch(err){
			//do nothing
		}
		return true;
	}else{
		try{
		tdiv.innerHTML = "Please enter a valid email";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		}catch(err){
			//do nothing
		}
		return false;
	}
}

function fnBeforePaste(){
	var swapCodes   = new Array(8211, 8212, 8216, 8217, 8220, 8221, 8226, 8230); // dec codes from char at
	var swapStrings = new Array("--", "--", "'",  "'",  '"',  '"',  "*",  "...");  
	// debug for new codes
    // for (i = 0; i < input.length; i++)  alert("'" + input.charAt(i) + "': " + input.charCodeAt(i));
    
    var output = window.clipboardData.getData("Text");
    for (i = 0; i < swapCodes.length; i++) {
        var swapper = new RegExp("\\u" + swapCodes[i].toString(16), "g"); // hex codes
        output = output.replace(swapper, swapStrings[i]);
    }
    window.clipboardData.setData("Text", output);
    return true;
}


function cleanWordClipboard(obj) {
	var swapCodes   = new Array(8211, 8212, 8216, 8217, 8220, 8221, 8226, 8230); // dec codes from char at
	var swapStrings = new Array("--", "--", "'",  "'",  '"',  '"',  "*",  "...");  
	// debug for new codes
    // for (i = 0; i < input.length; i++)  alert("'" + input.charAt(i) + "': " + input.charCodeAt(i));
    
    var output = obj.value;
    for (i = 0; i < swapCodes.length; i++) {
        var swapper = new RegExp("\\u" + swapCodes[i].toString(16), "g"); // hex codes
        output = output.replace(swapper, swapStrings[i]);
    }
    return obj.value = output;
}

function isPhoneNumber(phone){
	regphone = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i;
	return regphone.test(phone);
}

function format_phone(field_id){
	// Attempt to auto-format basic US phone numbers.  This method supports
	// 7 and 10 digit numbers.  Example: (410) 555-1212
	// Get the field that fired the event
	var oField = document.getElementById(field_id);
	
	// If we have the field and all is well
	if (typeof(oField) != "undefined" && oField != null){
		// Remove any non-numeric characters
		var sTmp = oField.value.replace(/[^0-9]/g, "");
		// If the number is a length we expect and support, format the number
		if(sTmp.length<10){
			document.getElementById(field_id + "err").innerHTML = "Invalid Phone";
			return;
		}
		//strip any preceeding 1's
		//if(sTmp.indexOf('1')==0){
		//	sTmp = sTmp.substr(1);
		//}
		//alert(sTmp);
		switch (sTmp.length){
			/*case "14105551212".length:
				oField.value = sTmp.substr(0,1) + "(" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
				document.getElementById(field_id + "err").innerHTML = "";
				break;*/
			case 9:
				oField.value = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
				document.getElementById(field_id + "err").innerHTML = "";				
				break;
			default:
				oField.value = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " " + sTmp.substr(10, sTmp.length-10);
				document.getElementById(field_id + "err").innerHTML = "";				
				break;
		}
	}
}


function isValidEmail(anEmail){
	//will check for valid email based on a passed value
	regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,3}$/i;
	return regEmail.test(anEmail);
}


function isValidPass(pass, cpass){
	if(pass.length<5){
		return false;
	}
	if(pass!=cpass){
		return false;
	}
	return true;
}

function checkPass(field, cfield, tdiv){
	
	tdiv = document.getElementById(tdiv)
	if(field.value.length<5){
		try{
		tdiv.innerHTML = "Password must be at least 5 characters";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		}catch(err){
			//do nothing
		}
	}else if(field.value != cfield.value){
		try{
		tdiv.innerHTML = "Passwords do not match";
		field.style.borderColor = "#FF0000";
		field.style.color = "Red";
		cfield.style.borderColor = "#FF0000";
		cfield.style.color = "Red";
		}catch(err){
			//do nothing
		}
	
	}else{
		try{
		tdiv.innerHTML = "";
		field.style.borderColor = "";
		field.style.color = "Black";
		cfield.style.borderColor = "";
		cfield.style.color = "Black";
		}catch(err){
			//do nothing
		}
	}
}

function hideDiv(divid){
	try{
		var tdiv = document.getElementById(divid);
		tdiv.style.display='none';
	}catch(e){}
}

function showDiv(divid){
	try{
		var tdiv = document.getElementById(divid);
		tdiv.style.display='block';
	}catch(e){}
}

function toggleDisplay(id){
	var targ = document.getElementById(id);
	if(targ.style.display=='none'){
		targ.style.display='';
	}else{
		targ.style.display='none';
	}
}

function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function highlightElement(element_id){
	//get element sizes
	if(document.getElementById('highligh_div_'+element_id)==null){
		var width = getElementWidth(element_id);
		var height = getElementHeight(element_id);
		var top = getElementTop(element_id);
		var left = getElementLeft(element_id);
		//drawdiv
		var newdiv = document.createElement('div');
		var divIdName = 'highligh_div_'+element_id;
		newdiv.setAttribute('id',divIdName);
		newdiv.style.width = width + "px";
		newdiv.style.height = height + "px";
		newdiv.style.top = top + "px";
		newdiv.style.left = left + "px";
		newdiv.style.opacity = .3;
		newdiv.style.position = 'absolute';
		newdiv.innerHTML="&nbsp;";
		newdiv.style.filter = 'alpha(opacity=30)';
		newdiv.style.background = '#FFFCCC';
		newdiv.style.zIndex = '18';
		newdiv.onMouseOut = "unhighlight('" + element_id + "')";
		document.body.appendChild(newdiv);
	}
}

function unhighlight(element_id){
	element = document.getElementById('highligh_div_'+element_id);
	
	if(element!=null){
		document.body.removeChild(element);
	}else{
		return false;
	}
}

function getElementHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}

function getElementWidth(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function getElementLeft(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageX;
	} else {
		var elem;
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		xPos = elem.offsetLeft;
		tempEl = elem.offsetParent;
  		while (tempEl != null) {
  			xPos += tempEl.offsetLeft;
	  		tempEl = tempEl.offsetParent;
  		}
		return xPos;
	}
}


function getElementTop(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.pageY;
	} else {
		if(document.getElementById) {	
			var elem = document.getElementById(Elem);
		} else if (document.all) {
			var elem = document.all[Elem];
		}
		yPos = elem.offsetTop;
		tempEl = elem.offsetParent;
		while (tempEl != null) {
  			yPos += tempEl.offsetTop;
	  		tempEl = tempEl.offsetParent;
  		}
		return yPos;
	}
}

function jspopup(location,name,width,height){
	window.open (location,name,'status=0,toolbar=0,menubar=0,directories=0,scrollbars=1,resizable=1,width='+width+',height='+height);
}

/************************************
*Application Specific Functions		*
*************************************/
//company section
function checkSalary(value){
	if(value=='Range'){
		
		$('amount_input').show();
		$('amount_input').removeClassName('multiselect-4');
		$('amount_input').addClassName('multiselect-6')
		$('amount_input').innerHTML = "<label for'salary1'>Salary Details</label><input type='text' id='salary1' name='salary1' /> - <input type='text' id='salary2' name='salary2' /> <select name='currency' id='currency'><option value='USD'>USD</option><option value='CDN' >CDN</option><option value='CNY'>CNY</option><option value='GBY'>GBY</option><option value='INR'>INR</option></select> Per <select name='salary_type' id='salary_type'><option value='hour'>Hour</option><option value='week'>Week</option><option value='month'>Month</option><option value='annum'>Annum</option></select>";
															
	}else if(value=='Amount'){
		document.getElementById('amount_input').show();
		$('amount_input').addClassName('multiselect-4');
		$('amount_input').removeClassName('multiselect-6')
		
		$('amount_input').innerHTML = "<label for'salary1'>Salary Details</label><input type='text' id='salary1' name='salary1' /><select name='currency' id='currency'><option value='USD'>USD</option><option value='CDN' >CDN</option><option value='CNY'>CNY</option><option value='GBY'>GBY</option><option value='INR'>INR</option></select> Per <select name='salary_type' id='salary_type'><option value='hour'>Hour</option><option value='week'>Week</option><option value='month'>Month</option><option value='annum'>Annum</option></select>";
	}else{
		if($('amount_input') != null){
			$('amount_input').hide();
		}
	}
}

function checkKeywords(str){
	var keywords = str.split(',');
	var retstr = "The Following Keywords Were Detected:<BR />";
	for (i=0; i<keywords.length; i++){
		retstr = retstr + keywords[i].substr(0, 25) + "<BR />";
	}
	document.getElementById('keyword_display').innerHTML = retstr;
}

// check or uncheck all textboxes:

function setCheckBoxes(formName, checkValue)
{
	if(!document.forms[formName])
		return;
	var inputs = document.forms[formName].getElementsByTagName("input");
	for (var i in inputs){
		var input = inputs[i];
		if (input.type == "checkbox"){
			input.checked = checkValue;
		}
	}
}

// check if the phone is valid
function checkPhone(phone)
{	
	if (phone.length > 0)
	{
		format_phone('phone');
	}
}

function checkAdvertiserPhone(phone)
{	
	if (phone.length > 0)
	{
		format_phone('contact_phone');
	}
}

function checkforwysiwyg(){

	var result = $$('.mceEditor');
	if(result.length>0){
		initwysiwyg();
	}
	var result2 = $$('.mceEditorro');
	if(result2.length>0){
		initwysiwygro();
	}
}
			
function initwysiwygro(){
	tinyMCE.init({
		// General options
		mode : "specific_textareas",
		theme : "advanced",
		readonly : 1,
		plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		//content_css : "/css/style.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"
		
	});
}

function initwysiwyg(){
	var mces = $$('.mceEditor');
	for (var i = 0; i<mces.length; i++){
		new Insertion.After(mces[i], "<input type='hidden' name='wysiwyg" + mces[i].name + "' value='true' />")
		mces[i].value = mces[i].value.replace(new RegExp( "\n", "g" ), "");
	}
	
	tinyMCE.init({
		// General options
		mode : "specific_textareas",
		editor_selector : "mceEditor",
		theme : "advanced",
		plugins : "safari,table,spellchecker,contextmenu,paste,nonbreaking,xhtmlxtras,template,inlinepopups",
		
		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink",
		theme_advanced_buttons3 : "spellchecker",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : false,

		paste_force_cleanup_wordpaste:true,
		paste_strip_class_attributes:'mso',
		paste_insert_word_content_callback:function (type, content) {
													switch (type) {
														case "before":
															break;
														case "after":
															content = content.replace(/<(!--)([\s\S]*)(--)>/gi, "");
															break;
														}
														return content;	
													},
		
		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		//content_css : "/css/style.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"

		// Replace values for the template plugin
	});
	//add hidden field to be submitted for check in php
	
	
	
}