var elmId = '';
var elmTxtId = '';
function checkNO(elmId, elmTxtId){
	if ($(elmId).checked == true){
			$(elmTxtId).disabled = false;
			$(elmTxtId).className = 'required';
			if (elmTxtId == "cbNO"){$('business_others').title = 'Please enter your business nature';}
			if (elmTxtId == "kf_search_txt"){$('kf_search_txt').title = 'Please enter your source';}
			if (elmTxtId == "kf_newspaper_txt"){$('kf_newspaper_txt').title = 'Please enter your source';}
			if (elmTxtId == "kf_others_txt"){$('kf_others_txt').title = 'Please enter your source';}
	}else{
			$(elmTxtId).disabled = true;
			$(elmTxtId).value = '';
			$(elmTxtId).className = null;		
			$(elmTxtId).title = '';
	}
}
		
var objPart = null;
var prodName = '_prod';
var delimiter = ', ';
var prod1ArrayA = new Array();
var prod1ArrayB = new Array();
var prod1ArrayC = new Array();
var prod1ArrayD = new Array();

var businessArrayA = new Array();
var businessArrayB = new Array();
var partElm = new Array();
var cbArray = new Array();
var divID = null;
var showNum = null;
var rowCount = 0;
var rowHtml = '';
var rowElm = '';
var lastRowElm ='';

function clearArray(clearArray){
	for (i=0; i<clearArray.length; i++){
		clearArray.pop();
	}
}

/*
 * Natural Sort algorithm for Javascript
 *  Version 0.2
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
 * Released under MIT license.
 */
function naturalSort (a, b) {
        // setup temp-scope variables for comparison evauluation
        var x = a.toString().toLowerCase() || '', y = b.toString().toLowerCase() || '',
                nC = String.fromCharCode(0),
                xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
                yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
                xD = (new Date(x)).getTime(), yD = (new Date(y)).getTime();
        // natural sorting of dates
        if ( xD && yD && xD < yD )
                return -1;
        else if ( xD && yD && xD > yD )
                return 1;
        // natural sorting through split numeric strings and default strings
        for ( var cLoc=0, numS = Math.max( xN.length, yN.length ); cLoc < numS; cLoc++ )
                if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) < ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
                        return -1;
                else if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) > ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
                        return 1;
        return 0;
}

function getValue(inputCatArray){
	tempArray = new Array();
	for (i=0; i<inputCatArray.length; i++){
		exist = false;
		for (j=0; j<tempArray.length; j++){
			if (tempArray[j] == inputCatArray[i]){
				exist = true;
			}
		}
		if (!exist){
			tempArray.push(inputCatArray[i]);
		}
	}
	return tempArray.join(delimiter);
}

function validateCheckBox(){
	var isChecked = false;
	for (var i = 0; i < $('regForm').prod.length; i++) {
	   if ($('regForm').prod[i].checked) {
		  isChecked = true;
	   }
	}
	
	if (isChecked == true){
		$('cat-msg').hide();
		$('submit-button').enable();
		$('submit-button').value = "Submit"
		return true;
	}else{
		$('cat-msg').show();
		$('submit-button').disable();
		$('submit-button').value = "Please select at least one interested product categories."
		return false;
	}
}

function appendOther(){
	$('prodCat').value = '';		//category #
	//$('prod1CatB').value = '';		//description
	$('prod1CatC').value = '';		//Main Cat #
	//$('prod1CatD').value = '';		// main cat desc
	
	$('businessCatA').value = '';	//business nature #
	$('businessCatB').value = '';	//description
		
	var seperator = '';
	
	if (!$('business_others').value.blank()){
		seperator = (getValue(businessArrayA) != '') ? ", " : "";
		$('businessCatA').value += getValue(businessArrayA.sort(naturalSort))+seperator+'14';
		$('businessCatB').value += getValue(businessArrayB)+seperator+$('business_others').value;
	}else {
		$('businessCatA').value += getValue(businessArrayA.sort(naturalSort));
		$('businessCatB').value += getValue(businessArrayB);
	}
	
	/*
	if (!$('prod1_others').value.blank()){
		seperator = (getValue(prod1ArrayA) != '') ? ", " : "";
		$('prod1CatA').value += getValue(prod1ArrayA.sort(naturalSort))+seperator+$('prod1_others').value;
		$('prod1CatB').value += getValue(prod1ArrayB)+seperator+$('prod1_others').value;
	}else {
		$('prod1CatA').value += getValue(prod1ArrayA.sort(naturalSort));
		$('prod1CatB').value += getValue(prod1ArrayB);
	}	
	
	if (!$('prod2_others').value.blank()){
		seperator = (getValue(prod1ArrayA.sort(naturalSort)) != '') ? ", " : "";
		$('prod1CatA').value += seperator+$('prod2_others').value;
		$('prod1CatB').value += seperator+$('prod2_others').value;
	}
	
	if (!$('prod3_others').value.blank()){
		seperator = (getValue(prod1ArrayA.sort(naturalSort)) != '') ? ", " : "";
		$('prod1CatA').value += seperator+$('prod3_others').value;
		$('prod1CatB').value += seperator+$('prod3_others').value;
	}
*/
	/*
	if (!$('prod4_others').value.blank()){
		seperator = (getValue(prod1ArrayA) != '') ? "," : "";
		$('prod1CatA').value += seperator+$('prod4_others').value;
		$('prod1CatB').value += seperator+$('prod4_others').value;
	}
	*/
	$('prodCat').value += getValue(prod1ArrayA.sort(naturalSort));
	$('prod1CatC').value += getValue(prod1ArrayC.sort(naturalSort));	
	//$('prod1CatD').value += getValue(prod1ArrayD);	


	//alert($('businessCatA').value);  
	//alert($('businessCatB').value); 	
	//alert($('prod1CatA').value);
	//alert($('prod1CatB').value);
	//alert($('prod1CatC').value);
	//alert($('prod1CatD').value);
			
	//append tel
	$('fulltel').value = '';
	$('fulltel').value += $('tel_country').value+''+$('tel').value;
	//append fax
	$('fullfax').value = '';
	$('fullfax').value += $('fax_country').value+''+$('fax').value;

	$('fullmobile').value = '';
	$('fullmobile').value += $('mobile_country').value+''+$('mobile').value;

	/*
	firstn = $('firstname0').value;
	lastn = $('lastname0').value;
	fullname = firstn+' '+lastn;
	$('fullname').value = fullname;
	
		for (i=1; i<=rowCount; i++){
			lastRowElm = 'fullname';
			first = 'firstname'+i;			
			last = 'lastname'+i;
			firstn = $(first).value;
			lastn = $(last).value;
			fullname = firstn+' '+lastn;
			rowHtml = '<input type="hidden" name="formData(fullname)" value="'+fullname+'" />';
			new Insertion.Before(lastRowElm, rowHtml);
		}
	*/
}

function checkItem(currentElement, arrayInput, arrayOutput){
	if (currentElement.checked){
		for (i=0; i<arrayInput.length; i++){
			arrayOutput.push(arrayInput[i]);
		}

	}else {
		for (i=0; i<arrayInput.length; i++){
			for (j=0; j<arrayOutput.length; j++){
				if (arrayOutput[j] == arrayInput[i]){
					var temp = arrayOutput[0];
					arrayOutput[0] = arrayOutput[j];
					arrayOutput[j] = temp;
					arrayOutput.shift();
					break;
				}
			}
		}
	}
}

// Add registration buyer
function addBuyerRow(){	
	lastRowElm = 'add_buyer';
	rowCount++;
	rowElm = 'buyer'+rowCount;		
	rowHtml = '<tr id="'+rowElm+'"><td colspan="2"><table cellpadding="2" cellspacing="0"><tr><td colspan="3" align="left" valign="center" bgcolor="#cccccc" class="preregistratable">Colleagues '+rowCount+'</td></tr><tr><td width="90" align="left" valign="center" class="preregistratable">Salutation :</td><td colspan="2" class="preregistratable"><select name="formData(title)" id="title'+rowCount+'" class="required"><option value=""> -- Please select a title --</option><option value="Mr.">Mr.</option><option value="Ms.">Ms.</option><option value="Mrs.">Mrs.</option><option value="Dr.">Dr.</option></select><img src="../../images/star2.gif" alt="star" width="7" height="7"></td></tr><tr><td align="left" valign="center" class="preregistratable"><span id="lblCFirstName2">First Name :</span></td><td width="213" class="preregistratable"><input name="formData(firstname)" maxlength="300" id="firstname'+rowCount+'" type="text" class="required validate-alpha"/><img src="../../images/star2.gif" alt="star" width="7" height="7"> </td><td width="349" class="preregistratable">Last Name :<input name="formData(lastname)" maxlength="300" id="lastname'+rowCount+'" type="text" class="required validate-alpha"/><img src="../../images/star2.gif" alt="star" width="7" height="7"></td></tr><tr><td align="left" valign="center" class="preregistratable">Job Title :</td><td colspan="2" class="preregistratable"><input name="formData(job_title)" maxlength="300" id="job_title'+rowElm+'" type="text" class="required validate-alpha"/><img src="../../images/star2.gif" alt="star" width="7" height="7"></td></tr><tr><td align="left" valign="center" class="preregistratable">Email :</td><td colspan="2" class="preregistratable"><input name="formData(email)" maxlength="300" id="email'+rowElm+'" type="text" class="required validate-email"/><img src="../../images/star2.gif" alt="star" width="7" height="7"></td></tr><tr><td align="left" valign="center" class="preregistratable">Email 2:</td><td colspan="2" class="preregistratable"><input name="formData(emailtwo)" maxlength="300" id="emailtwo'+rowElm+'" type="text" class="validate-email"/></td></tr></table></td></tr>';
	new Insertion.Before(lastRowElm, rowHtml);	
	checkAddRmRBuyerButton();	
}


function rmBuyerRow(){	
	$(rowElm).remove();
	rowCount--;
	rowElm = 'buyer'+rowCount;
	checkAddRmRBuyerButton();
}

function checkAddRmRBuyerButton(){
	if (rowCount > 9){
		//if row count is 9, disable the "invite more" button
		$('add_more').hide();
	}else{
		$('add_more').show();
	}
	
	if (rowCount <1){
		//if row count less than 2, disable the "remove" button
		$('remove').hide();
	}else{
		$('remove').show();
	}
}

function rld() {
	document.location.reload()
}

function clearForms()
{
  var i;
  for (i = 0; (i < document.forms.length); i++) {
    document.forms[i].reset();
  }
}


var inviteRow_count = 0;
var total_count = 0;
// Invite Buyer
function addInviteBuyerRow(){
	inviteRow_count++;
	total_count++;
	var keywordBlockElm = 'invite_'+inviteRow_count;
	var addRowId = 'invite_add_more_img';
	var removeHTML = '<td><input type="button" name="button3" id="remove_more" value="Remove" onclick="rmKeywordRow(\''+keywordBlockElm+'\');"/></td>';rowHtml = '<div style="padding:0;padding-top:5px;border:1px solid #aaa;" id="'+keywordBlockElm+'"><table style="padding:0;"><tr><td valign="top"></td><td valign="top"><span class="asterisk">*</span>(Buyer\'s Name)<br /><input type="text" name="formData(buyer_name)" size="25" value="" id="recipientName'+inviteRow_count+'" class="required" title="Please enter buyer\'s name"/><!-- replace:recipientName --></td><td valign="top">&nbsp;<span class="asterisk">*</span>(Buyer\'s Email)<br /><input type="text" name="formData(email)" size="25" value="" id="recipientEmail'+inviteRow_count+'" class="required validate-email" title="Please enter buyer\'s email"/></td>'+removeHTML+'</tr></table><br/></div>';

	new Insertion.Before(addRowId, rowHtml);
	checkAddRmBuyerButton();
	
}	

function rmKeywordRow(id){
	$(id).remove();
	total_count--;
	checkAddRmBuyerButton()
}

function checkChinaCountry(){
	if ($('country').value=="China"){
		$('china-msg').show();
		return true;
	}else{
		if ($('country').value=="Hong Kong"){
			$('hk-msg').show();
		}else{
			$('hk-msg').hide();
		}
		
		$('china-msg').hide();
		return true;
	}
}

function checkAddRmBuyerButton(){
	if (total_count > 9){
		//if row count is 9, disable the "invite more" button
		$('add_more').hide();
		//$('add_more_img').hide();
	}else{
		$('add_more').show();
		//$('add_more_img').show();
	}
}

var checked=0;
function checkEcardPass(){		
		var hashcode = 'ebb81f77497f491f865cba65a4571303';
		hash = hex_md5($F('passcode'));
		if(checked == 1){
			if (hashcode == hash){						
				$('submit_button').enable();
				$('submit_button').value = 'SEND';
				$('pass_msg').update('<span style="text-decoration:underline;font-size:10px;color:#009900;font-weight:bold;">Passcode Valid</span>');
				$('passed').value ='1';
			}else{					
				$('submit_button').disable();
				$('submit_button').value = 'Sorry! Your passcode is invalid.';
				$('pass_msg').update('<span style="text-decoration:underline;font-size:10px;color:#ff0000;font-weight:bold;">Invalid Passcode!</span>');
			}
		}
		checked=1;
}

function checkEcardPassTwo(){		
		var hashcode = 'ebb81f77497f491f865cba65a4571303';
		hash = hex_md5($F('passcode'));
		if(checked == 1){
			if (hashcode == hash){						
				$('submit_button').enable();
				$('submit_button').value = 'SEND';
				$('pass_msg').update('<span style="text-decoration:underline;font-size:10px;color:#009900;font-weight:bold;">Passcode Valid</span>');
				$('passed').value ='1';
			}else{					
				$('submit_button').disable();
				$('submit_button').value = 'Sorry! Your passcode is invalid.';
				$('pass_msg').update('<span style="text-decoration:underline;font-size:10px;color:#ff0000;font-weight:bold;">Invalid Passcode!</span>');
			}
		}
		checked=1;
}