/*Added for 1stdibs functionality ***KT***  August 16, 2010*/
	var num_items, flag_dibs,cnt_items;
	var num_fail;
	var failed_images;
	num_items=0;
	num_fails=0;
	failed_images=0;
	flag_dibs=0;
	cnt_items=0;

	/*Addition ends*/

function delete_warning(message, formelements, elementtype, elementname, elementnamelength)
{
	var selected_flag = 0;

	for (i = 0; i < formelements.length; i++){
		if(formelements[i].type == elementtype){
			if(formelements[i].name.substring(0, elementnamelength) == elementname){
				if(formelements[i].checked == true){
					selected_flag = selected_flag + 1;
				}
			}
		}
	}

	if(selected_flag == 0){
		alert("Please check atleast one Product.");
		return false;
	}

	var flag = confirm(message);
	
	if(flag == false){
		return false;
	}
}
/*new function added for press ***KT*** March 19,2010 */
function delete_warning_press(message, formelements, elementtype, elementname, elementnamelength)
{
	var selected_flag = 0;

	for (i = 0; i < formelements.length; i++){
		if(formelements[i].type == elementtype){
			if(formelements[i].name.substring(0, elementnamelength) == elementname){
				if(formelements[i].checked == true){
					selected_flag = selected_flag + 1;
				}
			}
		}
	}

	if(selected_flag == 0){
		alert("Please check atleast one Press item.");
		return false;
	}

	var flag = confirm(message);
	
	if(flag == false){
		return false;
	}
}

function delete_warning_single(message)
{
	var flag = confirm(message);
	
	if(flag == false){
		return false;
	}
}

function check_all(controllername, formelements, elementtype, elementname, elementnamelength)
{
	for (i = 0; i < formelements.length; i++){
		if(formelements[i].type == elementtype){
			if(formelements[i].name.substring(0, elementnamelength) == elementname){
				//alert (elementname);
				if(controllername.checked == true){
					formelements[i].checked = true;
				}else{
					formelements[i].checked = false;
				}
			}
		}
	}
}

function pausecomp(millis) 
{
	if(delay_check == 0){
		var date = new Date();
		var curDate = null;

		do{
			curDate = new Date();
		}while(curDate-date < millis);

		delay_check = 1;
	}
}

function validation()
{
	var val_str = "";	

	if(!is_not_empty(document.getElementById("product_name_id").value)){
		val_str += "PRODUCT NAME cannot be empty. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("product_name_id").value, 100)){
		val_str += "PRODUCT NAME length cannot exceed 100 characters. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("product_name_id").value)){
		val_str += "PRODUCT NAME cannot contain special characters. \n\n";		
	}

	if(document.getElementById("product_parent_id").selectedIndex == -1){
		val_str += "PRODUCT PARENT cannot be empty. \n\n";		
	}

	var prod_desc = FCKeditorAPI.GetInstance("product_desc").GetXHTML(true);

	var prod_desc = trim(prod_desc);

	if(!is_not_empty(prod_desc)){
		val_str += "PRODUCT DESCRIPTION cannot be empty. \n\n";		
	}

	if(!is_not_empty(document.getElementById("product_number_id").value)){
		val_str += "PRODUCT NUMBER cannot be empty. \n\n";		
	}	

	if(!is_less_than_length(document.getElementById("product_number_id").value, 6)){
		val_str += "PRODUCT NUMBER length cannot exceed 6 characters. \n\n";		
	}

	if(!is_alphanumeric(document.getElementById("product_number_id").value)){
		val_str += "PRODUCT NUMBER can only contain alphanumeric characters. \n\n";		
	}

	if(!is_numeric(document.getElementById("dimensions_length_id").value)){
		val_str += "DIMENSIONS LENGTH should be a number. \n\n";		
	}

	if(!is_numeric(document.getElementById("dimensions_width_id").value)){
		val_str += "DIMENSIONS WIDTH should be a number. \n\n";		
	}

	if(!is_numeric(document.getElementById("dimensions_height_id").value)){
		val_str += "DIMENSIONS HEIGHT should be a number. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("conditions_id").value)){
		val_str += "CONDITIONS cannot contain special characters. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("conditions_id").value, 256)){
		val_str += "CONDITIONS length cannot exceed 256 characters. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("product_year_id").value)){
		val_str += "PRODUCT YEAR cannot contain special characters. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("product_year_id").value, 75)){
		val_str += "PRODUCT YEAR length cannot exceed 75 characters. \n\n";		
	}	

	if(in_prohibited_array(document.getElementById("product_origin_id").value)){
		val_str += "PRODUCT ORIGIN cannot contain special characters. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("product_origin_id").value, 30)){
		val_str += "PRODUCT ORIGIN length cannot exceed 30 characters. \n\n";		
	}

	if(!is_not_empty(document.getElementById("product_title_name_id").value)){
		val_str += "PRODUCT TITLE cannot be empty. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("product_title_name_id").value, 100)){
		val_str += "PRODUCT TITLE length cannot exceed 100 characters. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("product_title_name_id").value)){
		val_str += "PRODUCT TITLE cannot contain special characters. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("product_meta_keywords_id").value)){
		val_str += "META KEYWORDS cannot contain special characters. \n\n";		
	}

	if(in_prohibited_array(document.getElementById("product_meta_desc_id").value)){
		val_str += "META DESCRIPTION cannot contain special characters. \n\n";		
	}

	if(!is_not_empty(document.getElementById("product_price_id").value)){
		val_str += "PRODUCT PRICE cannot be empty. \n\n";		
	}

	if(!is_less_than_length(document.getElementById("product_price_id").value, 15)){
		val_str += "PRODUCT PRICE length cannot exceed 15 characters. \n\n";		
	}

	if(!is_numeric(document.getElementById("product_price_id").value)){
		val_str += "PRICE should be a number. \n\n";		
	}

	//Validation check for the offer price field ***KT*** Jan 21, 2010

	if(!is_less_than_length(document.getElementById("product_offer_price_id").value, 15)){
		val_str += "PRODUCT SALE PRICE length cannot exceed 15 characters. \n\n";		
	}

	if(!is_numeric(document.getElementById("product_offer_price_id").value)){
		val_str += "SALE PRICE should be a number. \n\n";		
	}

	if(val_str.length > 0){
		alert(val_str);
		return false;
	}else{
		return true;
	}
}

function in_prohibited_array(string)
{
	var pro_array = new Array("!", "@", "#", "$", "%", "^", "*", "+", "|", "~", "`", "_");
	
	for(var j=0;j<string.length;j++){
		var temp_char = string.charAt(j);

		for(pro_array_char in pro_array){
			if(temp_char == pro_array[pro_array_char]){
				return true;
			}
		}
	}

	return false;
}

function is_not_empty(string)
{
	var trimmed_string = trim(string);

	if(trimmed_string.length <= 0){
		return 0;
	}else{
		return 1;
	}
}

function is_less_than_length(string, length)
{
	var trimmed_string = trim(string);

	if(trimmed_string.length <= length){
		return 1;
	}else{
		return 0;
	}
}

function is_alphanumeric(string)
{
	var numeric = string;

	for(var j=0;j<numeric.length;j++){
		var alphaa = numeric.charAt(j);

		var hh = alphaa.charCodeAt(0);

		if((hh > 47 && hh < 58) || (hh > 64 && hh < 91) || (hh > 96 && hh < 123)){
			/**/
		}else{
			return 0;	 
		}
 	}

	return 1;
}

function is_numeric(string)
{
	var numeric = string;
	var i;	
	var dot_count = 0;

	for(i = 0; i < numeric.length; i++){
		var calphaa = numeric.charAt(i);

		if(calphaa == "."){
			dot_count += 1;	
		}
	}

	for(var j=0;j<numeric.length;j++){
		var alphaa = numeric.charAt(j);

		var hh = alphaa.charCodeAt(0);

		if((hh > 47 && hh < 58) || (alphaa == "." && dot_count <= 1)){
			/**/
		}else{
			return 0;	 
		}
 	}

	return 1;
}

function trim(stringToTrim)
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/*For Ajax*/
function getXMLHttp()
{
  var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function MakeRequest(script_name)
{
  var xmlHttp = getXMLHttp();
  
  xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      //HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", script_name, true); 
  xmlHttp.send(null);
}

/*function HandleResponse(response)
{
  document.getElementById('ResponseDiv').innerHTML = response;
}*/

function disableEnterKey(e)
{
     var key;      
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox      

     return (key != 13);
}

/*Function to make an ajax call for 1stdibs ***KT*** August 11, 2010*/
function get_products_from_dibs()
{
	//alert('hi');
	var i;
	i=0;
	//var store_url=document.getElementById("store_url").value;
	//alert(store_url);
	var stuff=document.getElementById("product_ids_id").value
		//alert(stuff);
	if(stuff!="")
	{
		var stuff_arr=stuff.split(",");
	}
	else
	{
		alert('You must enter IDs of Products to be fetched from 1stdibs');
		exit;
	}
	var count=stuff_arr.length;
	//alert(count)
	document.getElementById('status_text').innerHTML="Processing";
	document.getElementById('prod_count').innerHTML="Total Products:"+count
	//document.getElementById('current_prod').innerHTML="Current Product:"	
	document.getElementById('added_prod').innerHTML="Fetching data from 1stdibs...This may take several minutes..."	
	flag_dibs=1;
	for(i=0;i<count;i++)
	{
			//alert(i);
			/*while(flag_dibs!=1)
			{
				document.getElementById('status_text').innerHTML=document.getElementById('status_text').innerHTML+".."
			}*/
			//document.getElementById('current_prod').innerHTML="Current Product:"+(i+1)
			if(stuff_arr[i]!="")
			{
				//alert('hi');
				//alert(stuff_arr[i]);
				/*if(flag_dibs==1)
				{*/
					//flag_dibs=0;
					/*flag_dibs=*/ajax_for_products(stuff_arr[i],count)
					//alert(flag_dibs)
					/*while(flag_dibs<1 || flag_dibs==null)
					{

						document.getElementById('status_text').innerHTML=document.getElementById('status_text').innerHTML+".."
					}*/
						//alert(flag_dibs)
				//}
				//alert(flag);
			}
	}
	/*while(flag!=1)
	{
		document.getElementById('status_text').innerHTML=document.getElementById('status_text').innerHTML+".."
	}
	if(flag==1)
	{*/
		document.getElementById('status_text').innerHTML="Processing Complete.";
	//}
}

function ajax_for_products(prod,count)
{

	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		//alert(xmlhttp.status);
		/*while(xmlhttp.status!=200)
		{
			document.getElementById('status_text').innerHTML=document.getElementById('status_text').innerHTML+".."
		}*/
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//alert(xmlhttp.responseText);
			if(xmlhttp.responseText == "1")
			{
				num_items++;
				document.getElementById('added_prod').innerHTML="Product(s) added successfully."
				cnt_items++;
			}
			else 
			if(xmlhttp.responseText == "0")
			{
				num_fails++
				document.getElementById('failed_prod').innerHTML="Failed to added some products."
				document.getElementById('added_prod').innerHTML="";
				cnt_items++;
			}
			else
			if (xmlhttp.responseText == "2")
			{
				document.getElementById('added_prod').innerHTML="Some product(s) had already been added at an earlier time and hence, were not added again to prevent a duplicate entry.";
				cnt_items++;
			}
			else
			{
				failed_images++;
				document.getElemenById('without_images').innerHTML="All products have been uploaded however, some product images could not be uploaded."
				cnt_items++;
			}
			//document.getElementById("status text").innerHTML="Products Added. Please verify if all the products have been added.";
			//alert(cnt_items)
			if(cnt_items==count)
			{
				//document.getElementById('added_prod').innerHTML="";
				alert('The mentioned products have been copied from 1stdibs.com. You must assign them a category and publish them to make them visible on the site.')
			}
		}
	}
		//alert("inside");
		xmlhttp.open("GET","http://www.mdantiques.com/product/data_sync/"+prod,true);
		//xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(null);
}